﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BelDecor.Net.Pages
{
    public partial class Servico : BasePage
    {

        #region EventosPagina
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ValidaSeguranca("Servico");
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            ResetaTela();
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            RadButton btn = ((RadButton)sender);
            int idServico = 0;
            Domain.Model.Servico servico = null;

            var valid = ValidaServico();

            if (valid == false)
                return;

            try
            {

                if (!string.IsNullOrEmpty(btn.CommandArgument))
                {
                    idServico = int.Parse(btn.CommandArgument.ToString());
                }

                if (idServico > 0)
                {
                    servico = Domain.Model.Servico.SingleOrDefault(f => f.Id_Servico == idServico);

                    if (servico != null)
                    {
                        SetAtributesServico(ref servico);
                    }
                    else
                    {
                        RadWindowManager1.RadAlert("Não foi possível encontrar os dados do Servico.", 0, 0, "Atenção", "");
                        return;
                    }
                }
                else
                {
                    //inclusão
                    servico = new Domain.Model.Servico();
                    SetAtributesServico(ref servico);
                }

                servico.Save();

                ResetaTela();
                PopulaGridServico(txtNomeServico.Text);
                RadWindowManager1.RadAlert("Operação concluída com sucesso.", 0, 0, "Atenção", "");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            ConfiguraInclusao();
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                PopulaGridServico(txtNomeServicoConsulta.Text);
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region Grid

        protected void grdServico_ItemCommand(object sender, GridCommandEventArgs e)
        {
            int idServico = int.Parse(e.CommandArgument.ToString());

            if (e.CommandName == "Editar")
            {
                PopulaEdicaoServico(idServico);
            }
            else if (e.CommandName == "Excluir")
            {
                ExcluirServico(idServico);
            }
        }

        #endregion

        #region Metodos
        void ConfiguraInclusao()
        {
            pnlIncluirEditar.Visible = true;
            btnSalvar.CommandArgument = string.Empty;
            txtNomeServico.Text = string.Empty;
            txtDescricao.Text = string.Empty;
            txtPreco.Value = null;

        }

        void ResetaTela()
        {
            pnlIncluirEditar.Visible = false;
            btnSalvar.CommandArgument = string.Empty;

            txtNomeServico.Text = string.Empty;
            txtDescricao.Text = string.Empty;
            txtPreco.Value = null;
        }

        bool ValidaServico()
        {
            if (string.IsNullOrEmpty(txtNomeServico.Text))
            {
                RadWindowManager1.RadAlert("Informe o campo Nome do Serviço.", 0, 0, "Atenção", "");
                pnlIncluirEditar.FocusControl(txtNomeServico);
                return false;
            }

            return true;
        }

        void SetAtributesServico(ref Domain.Model.Servico servico)
        {
            servico.tipo = txtNomeServico.Text;
            servico.descricao = txtDescricao.Text;
            servico.Preco = (decimal)txtPreco.Value;
        }

        void PopulaGridServico(string nome = "")
        {
            var lstServico = Domain.Model.Servico.All().Where(c =>
                                                            (!string.IsNullOrEmpty(nome.Trim()) ? c.tipo.Contains(nome.Trim()) : true)).ToList();

            grdServico.DataSource = lstServico;
            grdServico.DataBind();
            pnlGridBusca.Visible = true;
        }


        private void ExcluirServico(int idServico)
        {
            try
            {
                Domain.Model.Servico.Delete(c => c.Id_Servico == idServico);
                RadWindowManager1.RadAlert("Operação concluída com sucesso.", 0, 0, "Atenção", "");
                PopulaGridServico();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void PopulaEdicaoServico(int idServico)
        {
            var servico = Domain.Model.Servico.SingleOrDefault(f => f.Id_Servico == idServico);
            btnSalvar.CommandArgument = idServico.ToString();

            if (servico != null)
            {
                txtNomeServico.Text = servico.tipo;
                txtDescricao.Text = servico.descricao;
                txtPreco.Value = (double)servico.Preco;
                pnlIncluirEditar.Visible = true;
            }
        }
        #endregion

    }
}